#include "CSV.h"
#include <fstream>

CSV::CSV()
{
	m_nAllRowString.clear();
}

CSV::~CSV()
{
}

bool CSV::Init( const char* sFileName, char sToken /*= '\t'*/ )
{
	std::ifstream inFile( sFileName );
	if( !inFile )
		return false;

	m_sToken = sToken;
	std::string strLineContext;
	while( getline( inFile, strLineContext ) )
	{
		RowString aRowString;
		Splite( strLineContext, aRowString );
		m_nAllRowString.push_back( aRowString );
	}

	return false;
}

bool CSV::Splite( std::string& strLine, RowString& nRowString )
{
	int nBegin = 0;
	int nEnd = 0;

	while ( ( nEnd = strLine.find_first_of( m_sToken, nBegin ) ) != std::string::npos )
	{  
		nRowString.push_back( strLine.substr( nBegin, nEnd - nBegin ) );
		nBegin = nEnd + 1;
	}

	if ((nBegin = strLine.find_last_of( m_sToken, strLine.length() - 1 ) ) != std::string::npos )
	{
		nRowString.push_back( strLine.substr( nBegin + 1, strLine.length() - 1 ) );
	}

	return true;
}
